package com.put.mybills;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.ContentsResult;
import com.google.android.gms.drive.MetadataChangeSet;
import com.put.mybills.activities.CloudFragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

public class CreateFacturePhoto extends Activity {
	
	private static final String TAG = "CreateFacturePhoto";
	
	
	public static GoogleApiClient mGoogleApiClient;
	
	private Bitmap mBitmapToSave;
	
	private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
	private static final int REQUEST_CODE_CREATOR = 2;
	private static final int REQUEST_CODE_RESOLUTION = 3;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		//mGoogleApiClient = BaseDriveActivity.mGoogleApiClient;
		createPhoto();
		
	}


	public void  createPhoto() {
		
		Log.i(TAG, "API client connected.");
        if (mBitmapToSave == null) {
        	// This activity has no UI of its own. Just start the camera.
           startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                    REQUEST_CODE_CAPTURE_IMAGE);
            return;
        }
        saveFileToDrive("MyFacture");
		
	}
	

	 private void saveFileToDrive(final String name) {
	        // Start by creating a new contents, and setting a callback.
	        Log.i(TAG, "Creating new contents.");
	        final Bitmap image = mBitmapToSave;
	        Drive.DriveApi.newContents(mGoogleApiClient).setResultCallback(new ResultCallback<ContentsResult>() {

	            @Override
	            public void onResult(ContentsResult result) {
	                // If the operation was not successful, we cannot do anything
	                // and must
	                // fail.
	                if (!result.getStatus().isSuccess()) {
	                    Log.i(TAG, "Failed to create new contents.");
	                    return;
	                }
	                // Otherwise, we can write our data to the new contents.
	                Log.i(TAG, "New contents created.");
	                // Get an output stream for the contents.
	                OutputStream outputStream = result.getContents().getOutputStream();
	                // Write the bitmap data from it.
	                ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
	                //image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
	                try {
	                    outputStream.write(bitmapStream.toByteArray());
	                } catch (IOException e1) {
	                    Log.i(TAG, "Unable to write file contents.");
	                }
	                // Create the initial metadata - MIME type and title.
	                // Note that the user will be able to change the title later.
	                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
	                        .setMimeType("image/jpeg").setTitle(name).build();
	                // Create an intent for the file chooser, and start it.
	                IntentSender intentSender = Drive.DriveApi
	                        .newCreateFileActivityBuilder()
	                        .setInitialMetadata(metadataChangeSet)
	                        .setInitialContents(result.getContents())
	                        .build(mGoogleApiClient);
	                try {
	                    startIntentSenderForResult(
	                            intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
	                } catch (SendIntentException e) {
	                    Log.i(TAG, "Failed to launch file chooser.");
	                }
	            }
	        });
	    }	
	
	 @Override
	    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
	        switch (requestCode) {
	            case REQUEST_CODE_CAPTURE_IMAGE:
	                // Called after a photo has been taken.
	                if (resultCode == Activity.RESULT_OK) {
	                    // Store the image data as a bitmap for writing later.
	                    mBitmapToSave = (Bitmap) data.getExtras().get("data");
	                }
	                break;
	            case REQUEST_CODE_CREATOR:
	                // Called after a file is saved to Drive.
	                if (resultCode == RESULT_OK) {
	                    Log.i(TAG, "Image successfully saved.");
	                    mBitmapToSave = null;
	                    // Just start the camera again for another photo.
	                    
	                }
	                break;
	        }
    }

}
